package com.shyroke.daydayzhuansvc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.shyroke.daydayzhuanapi.dto.AdminUserDto;
import com.shyroke.daydayzhuanapi.dto.AdminUserRequest;
import com.shyroke.daydayzhuanapi.dto.CommitInfo;
import com.shyroke.daydayzhuanapi.entity.*;
import com.shyroke.daydayzhuanapi.mapper.UserMapper;
import com.shyroke.daydayzhuandao.respository.ProjectRepository;
import com.shyroke.daydayzhuandao.respository.SubmitArtRepository;
import com.shyroke.daydayzhuandao.respository.UserRepository;
import com.shyroke.daydayzhuansvc.util.Page;
import com.shyroke.daydayzhuansvc.util.TabelData;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserService {

    @Autowired
    private ProjectRepository projectRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private OSSService ossService;
    @Autowired
    private SubmitArtRepository submitArtRepository;
    @Autowired
    private JPAQueryFactory factory;
    @Autowired
    private UserMapper userMapper;

    /**
     * 获取投稿信息
     * @param uid
     * @return 投稿数、通过次数、总阅读数
     */
    public CommitInfo getCommitInfo(Integer uid) {
        //统计文章通过数
        Map<String,Object> totalMap = projectRepository.totalCountByUserId(uid);
        int pass = Integer.valueOf(totalMap.get("amount").toString());
        //阅读量
        int readCount = Integer.valueOf(totalMap.get("readSum").toString());;
        //统计不通过的文章数
        int noPass = submitArtRepository.totalNotPassCount(uid, 2);
        return CommitInfo.builder().noPass(noPass).readCount(readCount).pass(pass).build();
    }

    public UserEntity findById(Integer createId) {
        UserEntity userEntity = userRepository.findById(createId).get();
        userEntity.setTouxiang(ossService.getImgUrl(userEntity.getTouxiang()));
        return userEntity;
    }

    public TabelData<AdminUserDto> list(Page page, AdminUserRequest request) {
        QUserEntity qUserEntity = QUserEntity.userEntity;
        QUserRoleEntity qUserRoleEntity = QUserRoleEntity.userRoleEntity;
        JPAQuery<UserEntity> query = factory.selectFrom(qUserEntity).leftJoin(qUserRoleEntity).on(qUserEntity.id.eq(qUserRoleEntity.userid));
        if(StrUtil.isNotBlank(request.getUserName())){
            query = query.where(qUserEntity.name.like("%"+request.getUserName()+"%"));
        }
        if(StrUtil.isNotBlank(request.getAuthor())){
            query = query.where(qUserEntity.author.like("%"+request.getAuthor()+"%"));
        }
        QueryResults<UserEntity> queryResults = query
                .offset(PageRequest.of(page.getPage() - 1, page.getLimit()).getOffset()).limit(page.getLimit())
                .orderBy(qUserRoleEntity.roleid.asc(),qUserEntity.status.asc(),qUserEntity.id.desc())
                .fetchResults();

        List<AdminUserDto> list = queryResults.getResults().stream().map(e -> userMapper.entityToDto(e)).collect(Collectors.toList());
        return new TabelData(list,(int)queryResults.getTotal());
    }
}
